import java.text.SimpleDateFormat
import org.codehaus.groovy.grails.plugins.*

class CrawlerController {

    def sessionFactory

    def iall = {
        println "我唉中文字符"
        def dir = new File("D:\\projects\\groovy\\jj\\ii")
       
        def count = 0
        if (dir.exists()) {
       
            dir.eachFile{file ->
                
                if (true) { 

                //def file = new File("D:\\projects\\groovy\\jj\\ss\\6990.html")
                def text =  file.getText("GBK")
               
             
                    def post = new Post()
                    
                    post.title = getTag(text , '<font style="font-weight:bold;font-size:14px">' , '</td>')
                    post.content = getTag(text , '<td style="line-height:160%">' , '</td>')
                    def username = getTag(text , '发贴人：' , '\\(')
                     
                    def user = Student.findByUsername(username)
                    if (!user) {
                        user = Student.random.list()[0]
                    }
                    post.student = user
                    post.needSex = '不限'

                    def birthday = getTag(text , '发表时间：' , '人气:')
                     
                    try{
                    if (birthday != '' && birthday != '0-0') {  //2009-10-6 11:52:54
                         def dateFormat = new SimpleDateFormat ("yyyy-M-dhh:mm:ss"); 
                         post.createTime = dateFormat.parse(birthday)
                    }
                    }catch(Exception e){println "data error"}
                    if (post.createTime==null) {
                        post.createTime = new Date()
                    }

                    println post.toStr()
    //                student.discard()
                        try{
                            if (!post.save(flush: false)) {
                        
                                println file.name
                                post.errors.each{
                                    println it
                                }
                            }
                        }catch(Exception e){
                            println "##########Error"
                        }
                    }
                    count++
                    println count
//                    if (count%20 == 0) {
//                        System.gc()
//                        
//                            sessionFactory.currentSession.flush()
                            sessionFactory.currentSession.clear() 
                            DomainClassGrailsPlugin.PROPERTY_INSTANCE_MAP.get().clear() 
 
               
            }
        
        } 
        
        render("improt success")
    }

    def sall = {
        println "我唉中文字符"
        def dir = new File("D:\\projects\\groovy\\jj\\ss")
        def role  =   Role.findByAuthority("ROLE_STUDENT")
        def count = 0
        if (dir.exists()) {
       
            dir.eachFile{file ->
                
                if (true) { 

                //def file = new File("D:\\projects\\groovy\\jj\\ss\\6990.html")
                def text =  file.getText("GBK")
                def username =  getTag(text , '用户名：</td>' , '</td>')
                def student  = Student.findByUsername(username)
                if (!student) {
                    student = new Student()
                    
                    student.username = getTag(text , '用户名：</td>' , '</td>')
                    student.realname = getTag(text , '真实姓名：</td>' , '</td>')
                    student.sex = getTag(text , '性别：</td>' , '</td>')

                    def birthday = getTag(text , '出生年月：</td>' , '</td>')
                    try{
                    if (birthday != '' && birthday != '0-0') {
                         def dateFormat = new SimpleDateFormat ("yyyy-MM"); 
                         student.birthday = dateFormat.parse(birthday)
                    }
                    }catch(Exception e){println "data error"}
                    if (student.birthday==null) {
                        student.birthday = new Date()
                    }
                  
                    student.area =getTag(text , '所在地区：</td>' , '</td>')
                    student.tel = getTag(text , '联系电话：' , '<BR>')
                    student.mobile =getTag(text , '机：' , '<BR>')
                    student.qq = getTag(text , 'Ｑ：' , '<BR>')
                    student.email = getTag(text , 'Mail：' , '</td>')
                    student.address = getTag(text , '联系地址：</td>' , '</td>')
                    student.introduction = getTag(text , '补充说明：</td>' , '</td>')
                    student.addToAuthorities(role)

                   
                    
                    
    //                student.discard()
                        try{
                            if (!student.save(flush: false)) {
                                println student.toStr()
                                println file.name
                                student.errors.each{
                                    println it
                                }
                            }
                        }catch(Exception e){
                            println "##########Error"
                        }
                    }
                    count++
                    println count
//                    if (count%20 == 0) {
//                        System.gc()
//                        
//                            sessionFactory.currentSession.flush()
                            sessionFactory.currentSession.clear() 
                            DomainClassGrailsPlugin.PROPERTY_INSTANCE_MAP.get().clear() 
//                    }
                }
            }
        
        } 
        
        render("improt success")
    }
    def tall = {
        def dir = new File("D:\\projects\\groovy\\jj\\tt")
        def role  =   Role.findByAuthority("ROLE_TEACHER")
        def count = 0
        if (dir.exists()) {
       
            dir.eachFile{file ->
                
                if (true) { 

                //def file = new File("D:\\projects\\groovy\\jj\\ss\\6990.html")
                def text =  file.getText("GBK")
                def username =  getTag(text , '用户名：</td>' , '</td>')
                def teacher  = Teacher.findByUsername(username)
                if (teacher == null) {
                    teacher = new Teacher()
                  
                    teacher.username = getTag(text , '用户名：</td>' , '</td>')
                    teacher.realname = getTag(text , '真实姓名：</td>' , '</td>')
                    teacher.sex = getTag(text , '性别：</td>' , '</td>')

                    def birthday = getTag(text , '出生年月：</td>' , '</td>')
                    try{
                    if (birthday != '' && birthday != '0-0') {
                         def dateFormat = new SimpleDateFormat ("yyyy-MM"); 
                         teacher.birthday = dateFormat.parse(birthday)
                    }
                    }catch(Exception e){println "data error"}
                    if (teacher.birthday==null) {
                        teacher.birthday = new Date()
                    }
                  
                    teacher.area =getTag(text , '所在地区：</td>' , '</td>')
                    teacher.tel = getTag(text , '联系电话：' , '<BR>')
                    teacher.mobile =getTag(text , '机：' , '<BR>')
                    teacher.qq = getTag(text , 'Ｑ：' , '<BR>')
                    teacher.email = getTag(text , 'Mail：' , '</td>')
                    teacher.address = getTag(text , '联系地址：</td>' , '</td>')
                    teacher.introduction = getTag(text , '补充说明：</td>' , '</td>')


                teacher.major = getTag(text , '专业：</td>' , '</td>')
                teacher.education = getTag(text , '学历：</td>' , '</td>')
                teacher.unit = getTag(text , '现工作单位/学校：</td>' , '</td>')
                teacher.postion = getTag(text , '类型：</td>' , '</td>')
                teacher.teacherType = getTag(text , '教学场地：</td>' , '</td>')
                //hasMany = [courses:String]// courses可带科目
                teacher.experience = getTag(text , '家教经历：</td>' , '</td>')
                teacher.salary = getTag(text , '薪酬标准： </td>' , '</td>')
 
                 def shengren = getTag(text , '胜任科目：</td>' , '</b></td>')
                 println "====" + shengren
                 shengren.split(';').each{ccc ->
                    println ccc
                    if (ccc != '') {
                        teacher.addToCourses(ccc)
                    }
                   
                 }

                    teacher.addToAuthorities(role) 
    //                student.discard()
                        try{
                            if (!teacher.save(flush: true,failOnError :true)) {
                                println teacher.toStr()
                                println file.name
                                teacher.errors.each{
                                    println it
                                }
                            }
                        }catch(Exception e){
                            println "##########Error"
                        }
                        println count                    
                        //if (count%0 == 0) {
                            //System.gc()
                            //sessionFactory.currentSession.flush()
                            sessionFactory.currentSession.clear() 
                            DomainClassGrailsPlugin.PROPERTY_INSTANCE_MAP.get().clear() 
                        //}
                    }
                    count++
                    

                }
            }
        
        } 
        render("improt success")
    }


    def updatet = {
        def teachers = Teacher.list(sort: "id" , order:"desc")
        def dayz = 0
        def count = 0
        def now = new Date()
        java.util.Random random = new java.util.Random(); 
        teachers.eachWithIndex{ obj,i ->
            if (dayz == i) {
                count++
                dayz += 3+random.nextInt(5)
            }
            println "${i} ${count} ${dayz}" 
            println  (now - count)
            obj.createTime = (now - count)
            println obj.save(flush: true, failOnError :true)
//            sessionFactory.currentSession.flush() 
//            sessionFactory.currentSession.clear() 
            DomainClassGrailsPlugin.PROPERTY_INSTANCE_MAP.get().clear() 
        }

        render("update success")
    }
    def update2 = {
        def teachers = Teacher.findAllByCreateTimeBetween(new Date()-2 , new Date() ,[sort: "id" , order:"desc"])
        def dayz = 0
        def count = 0
        def now = new Date()
        java.util.Random random = new java.util.Random(); 
        teachers.eachWithIndex{ obj,i ->
            
                count++
              
            println "${i} ${count} ${dayz}" 
            println  (now - count)
            obj.createTime = (now - count)
            if(!obj.save(flush: true)){
                println obj
                obj.errors.each{
                    println it
                }
            }
//            sessionFactory.currentSession.flush() 
//            sessionFactory.currentSession.clear() 
            DomainClassGrailsPlugin.PROPERTY_INSTANCE_MAP.get().clear() 
        }

        render("update success")        
    }

    def updates = {
                def student = Student.list(sort: "id" , order:"desc")
        def dayz = 0
        def count = 0
        def now = new Date()
        java.util.Random random = new java.util.Random(); 
        student.eachWithIndex{ obj,i ->
            if (dayz == i) {
                count++
                dayz += 3+random.nextInt(5)
            }
            println "${i} ${count} ${dayz}" 
            println  (now - count)
            obj.createTime = (now - count)
            if(!obj.save(flush: true)){
                println obj
                obj.errors.each{
                    println it
                }
            }
//            sessionFactory.currentSession.flush() 
//            sessionFactory.currentSession.clear() 
            DomainClassGrailsPlugin.PROPERTY_INSTANCE_MAP.get().clear() 
        }

        render("update success")
    }

    def updatepw = {
        def user = User.list()
     
        user.eachWithIndex{ obj,i ->
            
            println "${i}" 
            if (obj.passwd.size()<= 6) {
                obj.passwd = authenticateService.encodePassword(obj.passwd)
            
                if(!obj.save(flush: true)){
                    println obj
                    obj.errors.each{
                        println it
                    }
                }
            }
//            sessionFactory.currentSession.flush() 
//            sessionFactory.currentSession.clear() 
            DomainClassGrailsPlugin.PROPERTY_INSTANCE_MAP.get().clear() 
        }

        render("update success")
    }



  def updaterole = {
        def user = User.list()
        def role  =   Role.findByAuthority("ROLE_TEACHER")
        println "=============${role}============="
     
        user.eachWithIndex{ obj,i ->
            
            println "${i}" 
            if (obj.authorities?.isEmpty()) {
                
                obj.addToAuthorities(role)
                if(!obj.save(flush: true)){
                    println obj
                    obj.errors.each{
                        println it
                    }
                }
            }
//            sessionFactory.currentSession.flush() 
//            sessionFactory.currentSession.clear() 
            DomainClassGrailsPlugin.PROPERTY_INSTANCE_MAP.get().clear() 
        }

        render("update success")
    }


    def authenticateService

    private getTag(String content, String start, String end) {
        String reg = start + "[^\f]*?" + end;
  
        def matcher = content  =~ reg
        String temp = ''
        try {
            if (matcher.size() > 0) {
                temp = matcher[0]
                temp = temp.replaceAll(start, "");
                temp = temp.replaceAll(end, "");
                temp = temp.replaceAll("<.*?>", "");
                temp = temp.replaceAll("&nbsp;", "");
                temp = temp.replaceAll(/\n|\t|\r| /, "");
   
            } 

        } catch (Exception e) {
             println("HtmlSense" + e);
        }
        return temp;
    } 
}
